import {app, BrowserWindow, ipcMain, screen,Tray,Menu} from 'electron'
import path from "path";
import {name} from '../renderer/util/app-data'
import {plugin} from '../renderer/util/electron-frameless-window-plugin'

plugin({
  setGlobal: true
})

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
global.__app_name = name
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
  global.__resources = path.resolve('./resources')
} else {
  global.__resources = path.resolve(__dirname, '../resources')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {

  const {width, height} = screen.getPrimaryDisplay().workAreaSize

  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    frame: false,
    transparent: true,
    hasShadow: false,
    width: width * 0.7,
    height: height * 0.8,
    minWidth: 800,
    minHeight: 600,
    // titleBarStyle: 'hidden-inset',
    title: __app_name,
    webPreferences: {
      plugins: true,
      nodeIntegration: true,
      webviewTag: true,
      defaultFontFamily: {
        'standard': 'Microsoft Yahei',
        'serif': 'Microsoft Yahei'
      }
    }

    // height: 600,
    // width: 800 ,
    // titleBarStyle: 'hidden-inset'
  })
  mainWindow.setMenu(null)
  mainWindow.loadURL(winURL)

  mainWindow.on('closed', () => {
    mainWindow = null
  })

  const appTray = new Tray(path.join(__resources, 'icons/icon.ico'));
  appTray.setToolTip(__app_name);
  appTray.setContextMenu(Menu.buildFromTemplate([
    {
      label: '退出',
      click: () => app.quit()
    }
  ]));
  appTray.on('click', () => mainWindow.show())
}
app.commandLine.appendSwitch('ppapi-flash-path', app.getPath('pepperFlashSystemPlugin'))

if (process.platform === 'win32'){
  process.env.COMSPEC = path.join(__resources, 'bin/cmd.exe')
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

/**
 * 用渲染子进程，向主进程发起通信，以实现用渲染出来的替换原有窗口的 最大化、最小化、关闭 按钮。
 * 这三个按钮使用渲染子进程做出来的，真正实现这些功能，还得让主进程来。所以需要向主进程发消息，来让主进程实现关闭
 */
function winClose() {
  // On OS X it is common for applications and their menu bar
  mainWindow.close()
}

function winMinimize() {
  mainWindow.minimize()
}

function winMaximize() {
  // console.log("isMaximized: "+mainWindow.isMaximized())
  if (mainWindow.isMaximized()) {
    mainWindow.unmaximize()
  } else {
    mainWindow.maximize()
  }
}

ipcMain.on('window-all-closed', (event, arg) => {
  winClose()// prints "ping"
})

ipcMain.on('window-all-minimize', (event, arg) => {
  winMinimize()// prints "ping"
})

ipcMain.on('window-all-maximize', (event, arg) => {
  winMaximize()// prints "ping"
})
